package example.service;

import com.sun.org.apache.bcel.internal.generic.NEW;
import example.dao.NormalUserMapper;
import example.model.NormalUser;
import example.model.NormalUserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by snksnk1091 on 2017/8/23.
 */
@Service
public class NormalUserServie {
    @Autowired
    private NormalUserMapper normalUserMapper;

    public long countNormalUserByName(String userName){
        NormalUserExample example = new NormalUserExample();
        example.createCriteria().andUserNameEqualTo(userName);
        return normalUserMapper.countByExample(example);
    }

    public long countNormalUserById(int id){
        NormalUserExample example = new NormalUserExample();
        example.createCriteria().andIdEqualTo(id);
        return normalUserMapper.countByExample(example);
    }

    public void insertNormalUserByNU(NormalUser normalUser){
        normalUserMapper.insertSelective(normalUser);
    }

    public NormalUser getNormalUserById(int id){
        NormalUserExample example = new NormalUserExample();
        example.createCriteria().andIdEqualTo(id);
        return normalUserMapper.selectByExample(example).get(0);
    }

    public void updateNormalUserByNU(NormalUser normalUser){
        NormalUserExample example = new NormalUserExample();
        example.createCriteria().andIdEqualTo(normalUser.getId());
        normalUserMapper.updateByExampleSelective(normalUser, example);
    }

    public void deleteNormalUserByName(String userName){
        NormalUserExample example = new NormalUserExample();
        example.createCriteria().andUserNameEqualTo(userName);
        normalUserMapper.deleteByExample(example);
    }

    public void changeMoney(String userName, double money){
        NormalUserExample example = new NormalUserExample();
        example.createCriteria().andUserNameEqualTo(userName);
        NormalUser normalUser = normalUserMapper.selectByExample(example).get(0);
        normalUser.setMoeny(money);
        normalUserMapper.updateByExampleSelective(normalUser, example);
    }

    public boolean checkPassword(String userName, String password){
        NormalUserExample example = new NormalUserExample();
        example.createCriteria().andUserNameEqualTo(userName).andPasswordEqualTo(password);
        return normalUserMapper.countByExample(example)>0;
    }

}
